<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>本地有约 - 登录</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary-color: #8aa899;
            --primary-dark: #6a8a7a;
            --primary-light: #a8c0b0;
            --secondary-color: #f5f5f5;
            --text-color: #333;
            --text-light: #666;
            --border-color: #ddd;
            --error-color: #e57373;
            --success-color: #81c784;
            --shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
            --radius: 12px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "Segoe UI", "Microsoft YaHei", sans-serif;
        }

        body {
            background: linear-gradient(135deg, #f8fbf9 0%, #e8f0ec 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            width: 100%;
            max-width: 420px;
        }

        .logo {
            text-align: center;
            margin-bottom: 40px;
        }

        .logo h1 {
            color: var(--primary-color);
            font-size: 36px;
            font-weight: 600;
            letter-spacing: 1px;
            margin-bottom: 12px;
        }

        .logo p {
            color: var(--text-light);
            font-size: 16px;
            font-weight: 400;
        }

        .card {
            background-color: white;
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            padding: 40px 35px;
            margin-bottom: 25px;
            position: relative;
            overflow: hidden;
        }

        .card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, var(--primary-color), var(--primary-light));
        }

        .card-header {
            margin-bottom: 30px;
            text-align: center;
        }

        .card-header h2 {
            color: var(--text-color);
            font-size: 26px;
            font-weight: 600;
            margin-bottom: 10px;
        }

        .card-header p {
            color: var(--text-light);
            font-size: 15px;
        }

        .form-group {
            margin-bottom: 24px;
        }

        .form-group label {
            display: block;
            margin-bottom: 10px;
            font-size: 14px;
            color: var(--text-color);
            font-weight: 500;
        }

        .input-with-icon {
            position: relative;
        }

        .input-with-icon i {
            position: absolute;
            left: 16px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--primary-color);
            font-size: 18px;
        }

        .input-with-icon input {
            padding-left: 50px;
        }

        .form-control {
            width: 100%;
            padding: 16px 18px;
            border: 2px solid var(--border-color);
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
            background-color: #fafafa;
        }

        .form-control:focus {
            outline: none;
            border-color: var(--primary-color);
            background-color: white;
            box-shadow: 0 0 0 4px rgba(138, 168, 153, 0.15);
        }

        .btn {
            display: block;
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 12px rgba(138, 168, 153, 0.3);
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(138, 168, 153, 0.4);
        }

        .btn:disabled {
            opacity: 0.7;
            cursor: not-allowed;
            transform: none;
            box-shadow: 0 4px 12px rgba(138, 168, 153, 0.3);
        }

        .links {
            text-align: center;
            margin-top: 25px;
        }

        .links a {
            color: var(--primary-color);
            text-decoration: none;
            font-size: 15px;
            font-weight: 500;
            transition: color 0.3s;
            margin: 0 8px;
        }

        .links a:hover {
            text-decoration: underline;
            color: var(--primary-dark);
        }

        .message {
            padding: 14px 18px;
            margin-bottom: 24px;
            border-radius: 10px;
            text-align: center;
            animation: fadeIn 0.5s ease-out;
            font-size: 15px;
            font-weight: 500;
        }

        .error {
            background-color: rgba(229, 115, 115, 0.1);
            color: var(--error-color);
            border: 2px solid rgba(229, 115, 115, 0.3);
        }

        .success {
            background-color: rgba(129, 199, 132, 0.1);
            color: var(--success-color);
            border: 2px solid rgba(129, 199, 132, 0.3);
        }

        .loading {
            background-color: rgba(138, 168, 153, 0.1);
            color: var(--primary-color);
            border: 2px solid rgba(138, 168, 153, 0.3);
        }

        .success-message {
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            background: var(--success-color);
            color: white;
            padding: 12px 24px;
            border-radius: 25px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
            z-index: 1000;
            animation: slideDown 0.5s ease-out;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-12px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @keyframes slideDown {
            from {
                opacity: 0;
                transform: translateX(-50%) translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateX(-50%) translateY(0);
            }
        }

        @media (max-width: 480px) {
            .card {
                padding: 30px 25px;
            }

            .logo h1 {
                font-size: 30px;
            }

            .card-header h2 {
                font-size: 22px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="logo">
            <h1>本地有约</h1>
            <p>发现身边精彩活动，遇见志同道合的人</p>
        </div>

        <div class="card">
            <div class="card-header">
                <h2>欢迎回来</h2>
                <p>使用用户名或邮箱登录您的账户</p>
            </div>

            {% if error %}
            <div class="message error">{{ error }}</div>
            {% endif %}

            {% if success %}
            <div class="message success">{{ success }}</div>
            {% endif %}

            <form method="POST" action="{% url 'customer:login' %}">
                {% csrf_token %}
                <div class="form-group">
                    <label for="username">用户名或邮箱</label>
                    <div class="input-with-icon">
                        <i class="fas fa-user"></i>
                        <input type="text" id="username" name="username" class="form-control" placeholder="请输入用户名或邮箱地址" required>
                    </div>
                </div>

                <div class="form-group">
                    <label for="password">密码</label>
                    <div class="input-with-icon">
                        <i class="fas fa-lock"></i>
                        <input type="password" id="password" name="password" class="form-control" placeholder="请输入密码" required>
                    </div>
                </div>

                <button type="submit" class="btn" id="submitBtn">登录</button>
            </form>

            <div class="links">
                <a href="{% url 'customer:forgot_password' %}">忘记密码？</a>
                <span> | </span>
                <a href="{% url 'customer:register' %}">注册新账户</a>
            </div>
        </div>
    </div>

    <script>
    // 获取 CSRF 令牌的函数
    function getCSRFToken() {
        return document.querySelector('[name=csrfmiddlewaretoken]').value;
    }

    // 验证用户名或邮箱格式
    function validateUsernameOrEmail(value) {
        // 用户名规则：字母、数字、下划线，长度3-20
        const usernameRegex = /^[a-zA-Z0-9_]{3,20}$/;

        // 邮箱验证规则
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

        return usernameRegex.test(value) || emailRegex.test(value);
    }

    // 显示成功消息
    function showSuccessMessage(message) {
        const successDiv = document.createElement('div');
        successDiv.className = 'success-message';
        successDiv.innerHTML = `
            <i class="fas fa-check-circle me-2"></i>
            ${message}
        `;
        document.body.appendChild(successDiv);

        // 3秒后自动移除
        setTimeout(() => {
            successDiv.remove();
        }, 3000);
    }

    // 显示错误消息
    function showErrorMessage(message) {
        // 先移除可能存在的成功消息
        const existingSuccess = document.querySelector('.success-message');
        if (existingSuccess) {
            existingSuccess.remove();
        }

        // 在表单上方显示错误消息
        const form = document.querySelector('form');
        const existingError = document.querySelector('.message.error');

        if (existingError) {
            existingError.textContent = message;
            existingError.style.display = 'block';
        } else {
            const errorDiv = document.createElement('div');
            errorDiv.className = 'message error';
            errorDiv.textContent = message;
            form.insertBefore(errorDiv, form.firstChild);
        }
    }

    // 显示加载消息
    function showLoadingMessage() {
        const form = document.querySelector('form');
        const existingMessages = document.querySelectorAll('.message');
        existingMessages.forEach(msg => msg.style.display = 'none');

        const loadingDiv = document.createElement('div');
        loadingDiv.className = 'message loading';
        loadingDiv.innerHTML = '<i class="fas fa-spinner fa-spin me-2"></i>登录中...';
        loadingDiv.id = 'loadingMessage';
        form.insertBefore(loadingDiv, form.firstChild);
    }

    document.querySelector('form').addEventListener('submit', function(e) {
        e.preventDefault();

        const usernameInput = document.getElementById('username');
        const usernameValue = usernameInput.value.trim();

        // 验证用户名或邮箱格式
        if (!validateUsernameOrEmail(usernameValue)) {
            showErrorMessage('请输入有效的用户名或邮箱地址');
            usernameInput.focus();
            return;
        }

        // 显示加载状态
        const submitBtn = document.getElementById('submitBtn');
        const originalText = submitBtn.textContent;
        submitBtn.disabled = true;
        submitBtn.textContent = '登录中...';

        // 显示加载消息
        showLoadingMessage();

        const formData = new FormData(this);

        fetch(this.action, {
            method: 'POST',
            body: formData,
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'X-CSRFToken': getCSRFToken()
            }
        })
        .then(response => {
            if (!response.ok) {
                throw new Error('网络响应不正常');
            }
            return response.json();
        })
        .then(data => {
            if (data.success) {
                // 显示成功消息
                showSuccessMessage(data.message);

                // 根据用户类型进行跳转
                setTimeout(() => {
                    window.location.href = data.redirect_url;
                }, 1500);
            } else {
                // 显示错误消息
                showErrorMessage(data.message);
                submitBtn.disabled = false;
                submitBtn.textContent = originalText;

                // 移除加载消息
                const loadingMsg = document.getElementById('loadingMessage');
                if (loadingMsg) {
                    loadingMsg.remove();
                }
            }
        })
        .catch(error => {
            console.error('Error:', error);
            showErrorMessage('网络错误，请重试');
            submitBtn.disabled = false;
            submitBtn.textContent = originalText;

            // 移除加载消息
            const loadingMsg = document.getElementById('loadingMessage');
            if (loadingMsg) {
                loadingMsg.remove();
            }
        });
    });

    // 输入框获取焦点时恢复默认样式
    const inputs = document.querySelectorAll('.form-control');
    inputs.forEach(input => {
        input.addEventListener('focus', function() {
            this.style.borderColor = 'var(--primary-color)';
            this.style.boxShadow = '0 0 0 4px rgba(138, 168, 153, 0.15)';

            // 清除错误消息
            const errorMsg = document.querySelector('.message.error');
            if (errorMsg) {
                errorMsg.remove();
            }
        });

        input.addEventListener('blur', function() {
            if (!this.value) {
                this.style.borderColor = 'var(--border-color)';
                this.style.boxShadow = 'none';
            }
        });
    });

    // 用户名输入框实时验证
    const usernameInput = document.getElementById('username');
    usernameInput.addEventListener('blur', function() {
        const value = this.value.trim();
        if (value && !validateUsernameOrEmail(value)) {
            showErrorMessage('请输入有效的用户名或邮箱地址');
        }
    });

    // 回车键提交表单
    document.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            const focusedElement = document.activeElement;
            if (focusedElement && focusedElement.form) {
                focusedElement.form.dispatchEvent(new Event('submit', { cancelable: true }));
            }
        }
    });

    // 页面加载时隐藏可能存在的加载消息
    document.addEventListener('DOMContentLoaded', function() {
        const loadingMsg = document.getElementById('loadingMessage');
        if (loadingMsg) {
            loadingMsg.remove();
        }
    });
</script>
</body>
</html>